<template>
 <div id="app">
      <div class="contain">
        <!-- 左侧列表 -->
        <div class="list-box">

          <!-- 添加资产 -->
          <form class="my-form">
            <input type="text" class="form-control" placeholder="消费名称" v-model="name"/>
            <input type="text" class="form-control" placeholder="消费价格" v-model="price"/>
            <button type="button" class="btn btn-primary"  @click="addItem">添加账单</button>

          </form>

          <table class="table table-hover">
            <thead>
              <tr>
                <th>编号</th>

                <th>消费名称</th>

                <th>消费价格</th>

                <th>操作</th>

              </tr>

            </thead>

            <tbody>
              <tr v-for="(item,index) in list" :key="item.id">
                <td>{{item.id}}</td>

                <td>{{item.name}}</td>

                <td class="price">{{item.price}}</td>

                <td><a href="javascript:;" @click="deleteItem(index,item.id)">删除</a></td>

              </tr>

            </tbody>

            <tfoot>
              <tr>
                <td colspan="4">消费总计： {{totalPrice}}</td>

              </tr>

            </tfoot>
          </table>
<!-- 饼图组件 -->
        <PieChart :chartData="list" />
        </div>
      </div>
    </div>

</template>

<script>
import axios from 'axios';
import PieChart from './components/PieChart.vue';
export default {
    //data数据必须是函数，不然会报错
    data() {
    return {
      //1、定义数组list，保存数据
      list: [],
      name: '',
      price: '',
      }
  },
  components: {
    PieChart,
  },
  async created() {
    //在页面加载时，向后台发送请求，获取数据
    const res = await axios({url:'https://applet-base-api-t.itheima.net/bill',params:{ creator:'yyy1' }})
    this.list = res.data.data
    const res1 = await axios({url:'https://applet-base-api-t.itheima.net/api/translate'})
    console.log(res1.data)
  },
  //函数
  methods: {
    async deleteItem(index,id) {
      try {
        //发送删除请求
        await axios.delete(`https://applet-base-api-t.itheima.net/bill/${id}`)
      } catch (error) {
        console.log('删除失败')
      }
      this.list.splice(index,1)
    },
    async addItem() {
      if (!this.name.trim() || !this.price.trim()) {
        alert('请输入消费名称和价格')
      return 
      }
    
      try {
        //发送添加请求
        await axios.post('https://applet-base-api-t.itheima.net/bill',{creator:'yyy1',name:this.name,price:this.price})
      } catch (error) {
        console.log('添加失败')
      }
      //重新获取
      const res = await axios({ url: 'https://applet-base-api-t.itheima.net/bill', params: { creator: 'yyy1' } })
      this.list = res.data.data
      this.name = ''
      this.price = ''
    }
  },
  //计算属性
  computed:{
totalPrice(){
  return this.list.reduce((pre,item)=>{
    return pre + item.price
  },0)
}
  },
  watch:{
  }

  }
</script>

    <style>
    .price:hover{
    color:  red !important;
    }
      .red {
        color: red!important;
      }
      .search {
        width: 300px;
        margin: 20px 0;
      }
      .my-form {
        display: flex;
        margin: 20px 0;
      }
      .my-form input {
        flex: 1;
        margin-right: 20px;
      }
      .table > :not(:first-child) {
        border-top: none;
      }
      .contain {
        display: flex;
        padding: 10px;
      }
      .list-box {
        flex: 1;
        padding: 0 30px;
      }
      .list-box  a {
        text-decoration: none;
      }
      .echarts-box {
        width: 600px;
        height: 400px;
        padding: 30px;
        margin: 0 auto;
        border: 1px solid #ccc;
      }
      tfoot {
        font-weight: bold;
      }
      @media screen and (max-width: 1000px) {
        .contain {
          flex-wrap: wrap;
        }
        .list-box {
          width: 100%;
        }
        .echarts-box {
          margin-top: 30px;
        }
      }
    </style>
vba